/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.sondx.convertcsv;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import csv.TableReader;
import csv.impl.CSVReader;

public class FileConverter {

    @SuppressWarnings("deprecation")
    public static int ConvertCSVToXLS(String filecvs, String filexls, boolean firstHeader) {
        try {
            ArrayList<ArrayList<Object>> arList = new ArrayList<ArrayList<Object>>();
            ArrayList<Object> al;
            CSVReader csv = new CSVReader(filecvs);
            csv.setHasHeaderRow(firstHeader);
            csv.setColumnSeparator(',');
            TableReader in = csv;
            while (in.hasNext()) {
                al = new ArrayList<Object>();
                Object columns[] = in.next();
                for (int j = 0; j < columns.length; j++) {
                    Object edit = columns[j];
                    al.add(edit);
                }
                arList.add(al);
            }
            in.close();

            HSSFWorkbook hwb = new HSSFWorkbook();
            HSSFSheet sheet = hwb.createSheet("Hoja 1");
            HSSFCellStyle cellStyle = hwb.createCellStyle();
            HSSFDataFormat dataFormat = hwb.createDataFormat();
            cellStyle.setDataFormat(dataFormat.getFormat("dd/mm/yyyy"));
            for (int k = 0; k < arList.size(); k++) {
                ArrayList<Object> ardata = arList.get(k);
                HSSFRow row = sheet.createRow((short) 0 + k);
                for (int p = 0; p < ardata.size(); p++) {
                    HSSFCell cell = row.createCell((short) p);
                    decideDoStuff(cell, ardata.get(p), cellStyle);
                }
            }
            FileOutputStream fileOut = new FileOutputStream(filexls);
            hwb.write(fileOut);
            fileOut.close();
            return 1;
        } catch (IOException ex) {
            Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, ex.getLocalizedMessage(), "Error Interno", JOptionPane.ERROR_MESSAGE);
            return 0;
        }

    }

    public static void decideDoStuff(HSSFCell cell, Object o, HSSFCellStyle cellStyle) {

        String DATE_FORMAT = "MM/dd/yyyy";
        DateFormat sdf = new SimpleDateFormat(DATE_FORMAT);

        try {
            cell.setCellValue(Integer.valueOf(o.toString()));
        } catch (NumberFormatException e) {
            try {
                cell.setCellValue(Double.valueOf(o.toString()));
            } catch (NumberFormatException ed) {
                try {
                    cell.setCellValue(Float.valueOf(o.toString()));
                } catch (NumberFormatException ef) {
                    try {
                        cell.setCellStyle(cellStyle);
                        cell.setCellValue(sdf.parse(o.toString()));
                    } catch (ParseException edt) {

                        cell.setCellValue(o.toString());
                    }
                }
            }
        }
    }
}
